/**
 *\*\file log.c
 *\*\author Nations
 *\*\version v1.0.0
 *\*\copyright Copyright (c) 2019, Nations Technologies Inc. All rights reserved.
 **/
#include "bsp_4g_task.h"
#include "bsp_4g_common.h"
#include "msg_package.h"

#if PRINT_DEBUG
#define PRINTF(fmt, ...) printf(fmt, ##__VA_ARGS__)
#else
#define PRINTF(fmt, ...)
#endif

/** 服务器连接参数请求
 * @brief
 *
 * @return _server_connct_step_enum
 */
_server_connct_step_enum server_connect(void)
{

#define HTP_TIME_OUT(times)            \
    if (device_4g->retytimes >= times) \
        return server_connct_step;

#define HTP_NEXT_STEP(X)          \
    {                             \
        server_connct_step = X;   \
        device_4g->retytimes = 0; \
    }

    int relength = 0;
    int resperr = 0, respcode = 0, resplen = 0;
    rec_4G_unit *rec = NULL;
    _server_connct_step_enum server_connct_step = HTTP_REQ_RESET;
    while (1)
    {
        switch (server_connct_step)
        {
        case HTTP_REQ_RESET:
            rec = SEND_CMD("AT+QHTTPCFG=\"reset\"\r\n", 1000);
            if (rec)
            {
                DATA_TRIM(rec);
                if (RT_NULL != rt_strstr(rec->buf, RE_OK))
                {
                    PRINTF("HTTP_REQ_RESET:%s\n", rec->buf);
                    HTP_NEXT_STEP(HTTP_REQHEADER_ENALBE);
                }
            }
            HTP_TIME_OUT(2);
            break;
        case HTTP_REQHEADER_ENALBE:
            rec = SEND_CMD("AT+QHTTPCFG=\"requestheader\",0\r\n", 1000);
            if (rec)
            {
                DATA_TRIM(rec);
                if (RT_NULL != rt_strstr(rec->buf, RE_OK))
                {
                    PRINTF("HTTP_REQHEADER_ENALBE:%s\n", rec->buf);
                    HTP_NEXT_STEP(HTTP_RSPHEADER_ENABLE);
                }
            }
            HTP_TIME_OUT(2);
            break;
        case HTTP_RSPHEADER_ENABLE:
            rec = SEND_CMD("AT+QHTTPCFG=\"responseheader\",0\r\n", 1000);
            if (rec)
            {
                DATA_TRIM(rec);
                if (RT_NULL != rt_strstr(rec->buf, RE_OK))
                {
                    PRINTF("HTTP_RSPHEADER_ENABLE:%s\n", rec->buf);
                    HTP_NEXT_STEP(HTTP_CFG_URL_START);
                }
            }
            HTP_TIME_OUT(2);
            break;
        case HTTP_CFG_URL_START:
        {
            char temp[32];
            relength = rt_snprintf(temp, 32, "AT+QHTTPURL=%d,80\r\n", rt_strlen(http_server_host));
            temp[relength] = '\0';
            rec = SEND_CMD(temp, 1000);
            if (rec)
            {
                DATA_TRIM(rec);
                if (RT_NULL != rt_strstr(rec->buf, CONNECT))
                {
                    PRINTF("HTTP_CFG_URL_START:%s\n", rec->buf);
                    HTP_NEXT_STEP(HTTP_CFG_URL_DATA);
                }
            }
            HTP_TIME_OUT(2);
            break;
        }
        case HTTP_CFG_URL_DATA:
            rec = SEND_CMD(http_server_host, 1000);
            if (rec)
            {
                DATA_TRIM(rec);
                if (RT_NULL != rt_strstr(rec->buf, RE_OK))
                {
                    PRINTF("HTTP_CFG_URL_DATA:%s\n", rec->buf);
                    //                    if (device_4g->cache == RT_NULL) // 生产测试任务
                    //                    {
                    //                        device_4g->link_sta = 1; // 只要HTTP有回复，即可证明设备可运行
                    //                        return HTTP_PARA_FACTORY_TEST;
                    //                    }
                    //                    else
                    {
                        HTP_NEXT_STEP(HTTP_REQ_POST_START);
                    }
                }
            }
            HTP_TIME_OUT(2);
            break;
        case HTTP_REQ_GET_SATRT:
            break;
        case HTTP_REQ_GET_DATA:
            break;
        case HTTP_REQ_POST_START:
        {
            //  组发送报文
            char temp[32];
            int relength = rt_snprintf(temp, 32, "AT+QHTTPPOST=%d,80,80\r\n", get_AllMSG_length(device_4g->current_write->buf, 1, device_4g)); // 获取要发送的数据长度
            temp[relength] = '\0';
            // 发送，阻塞等待
            rec = SEND_CMD(temp, 3000);
            if (rec)
            {
                DATA_TRIM(rec);
                if (RT_NULL != rt_strstr(rec->buf, CONNECT))
                {
                    PRINTF("HTTP_REQ_POST_START:%s\n", rec->buf);
                    HTP_NEXT_STEP(HTTP_REQ_POST_DATA);
                }
            }
            HTP_TIME_OUT(3);
            break;
        }
        case HTTP_REQ_POST_DATA:
            get_AllMSG_length(device_4g->current_write->buf, 1, device_4g); // 再次组包发送数据
            printf("%s.\r\n", device_4g->current_write->buf);
            device_4g->mqtt_data_sta = 1;
            rec = SEND_CMD_DIRECT(device_4g->current_write->buf, 1000);
            if (rec)
            {
                DATA_TRIM(rec);
                if (RT_NULL != rt_strstr(rec->buf, RE_OK))
                {
                    PRINTF("HTTP_REQ_POST_DATA:%s\n", rec->buf);
                    HTP_NEXT_STEP(HTTP_RESP_LISTEN);
                }
            }
            HTP_TIME_OUT(2);
            break;
        case HTTP_RESP_LISTEN:
            rec = DATA_LISTEN(5000);
            if (rec)
            {
                device_4g->mqtt_data_sta = 1;
                DATA_TRIM(rec);
                PRINTF("HTTP_RESP_LISTEN:%s\n", rec->buf);
                if (RT_NULL != rt_strstr(rec->buf, "+QHTTPPOST:"))
                {
                    if (3 == sscanf(rec->buf, (char const *)"\r\n+QHTTPPOST: %d,%d,%d", &resperr, &respcode, &resplen))
                    {
                        if (resperr == 0 && respcode == 200)
                        {
                            HTP_NEXT_STEP(HTTP_RESP_DATA_READ);
                        }
                        else
                            return server_connct_step;
                    }
                }
            }
            HTP_TIME_OUT(3);
            break;
        case HTTP_RESP_DATA_READ:
            rec = SEND_CMD("AT+QHTTPREAD=80\r\n", 1000);
            if (rec)
            {
                DATA_TRIM(rec);
                char *tempstr = rt_strstr(rec->buf, CONNECT);
                if (RT_NULL != tempstr)
                {
                    tempstr += rt_strlen(CONNECT);
                    PRINTF("HTTP_RESP_DATA_READ:%d \n", rt_strnlen(rec->buf, 1024));
                    return HTTP_PRAR_READ_FAILED;

                    //					if (0 == http_connect_decode((void *)&(device_4g->DTU_para), tempstr, relength))
                    //                        return HTTP_PRAR_READ_SUCCESS;
                    //                    else
                    //                        return HTTP_PRAR_READ_FAILED;
                }
            }
            HTP_TIME_OUT(2);
            break;

        default:
            break;
        }
    }
}
